<?php
/**
 * Description of AuditModule
 * 用户审核Module
 * @author
 */
//require_rewrite(MODULE_ROOT.'dao/UserDao.class.php');
class AuditModule extends BaseModule{


    //审核ID
    private $audit_id;

    public function __construct($audit_id) {
        parent::__construct();
        $this->audit_id=$audit_id;
    }

    //关键词搜索条件
    private function init($sel_user) {
        $where='';
        if (!empty($sel_user['txtusername'])) {
                $where.= "username = '" . $sel_user['txtusername']."'";
        }
        if(!empty($where)){
            return $where;
        }
        return 1;
    }

//    /**
//     * 查找所有被锁定的管理员
//     */
//    public function sql_audit_user($sql =''){
//        $list=UserDao::sqlAuditUser($this->audit_id,$this->init($sql));
//        if(!empty($list)){
//            foreach ($list as $key=>$val){
//                   $jsonData[$key]['userid'] = $val['userid'];
//                   $jsonData[$key]['id'] = $val['id'];
//                   $jsonData[$key]['audit_aion'] = $val['audit_aion'];
//                   $jsonData[$key]['log_id'] = $val['log_id'];
//                   $jsonData[$key]['nickname']=base64_decode($val['nickname']);
//                if(!empty($val['json_data'])){
//                    $jsonData[$key]['user']=$this->from_json($val['json_data']);
//                }
//                switch ($val['audit_aion']){
//                    case '1':
//                        $jsonData[$key]['user']['auditAion'] = "新添加该用户";
//                        break;
//                    case '2':
//                        $jsonData[$key]['user']['auditAion'] = "更新该用户";
//                        break;
//                    case '3':
//                        $jsonData[$key]['user']['auditAion'] = "删除该用户";
//                        break;
//                    case '4':
//                        $jsonData[$key]['user']['auditAion'] = "密码输入错误超过规定";
//                        break;
//                }
//            }
//            foreach($jsonData as $k=>$row){
//                $dep=UserDao::getDepartmentList($row['user']['department']);
//                $jsonData[$k]['user']['depName']=$dep[0]['nickname'];
//                //$jsonData[$k]['user']['nick_name']= base64_decode($dep[0]['nickname']);
//            }
//            return $jsonData;
//        }
//        return false;
//    }

     /**
     * 查找所有被锁定的管理员
     */
    public function sql_admin_user($sql =''){
        $list=UserDao::sqlAdminUser($this->init($sql));
        if(!empty($list)){
            return $list;
        }
        return false;
    }
    /**
     * 将JSON信息转换成数组格式
     * @param json $json_data 用户信息
     * @return array
     */
    private function from_json($json_data){
        if(!empty($json_data)){
            $json = explode(',',str_replace(array("{","}"), "", $json_data));
            foreach($json as $val)
            {
                $ex=explode(':',str_replace("\"",'', $val));
                $data[$ex[0]] = $ex[1];
            }
              return $data;
        }
        return false;
    }

    /**
     * 用户更新与添加
     * @param type $jsondata
     * @return string
     */
    private function sql_auth_where($type=''){
        $detail=$this->sql_audit_user();
        $group=UserDao::getDepartmentList($detail[0]['user']['department']);
        $groupname=$group[0]['nickname'];
        if(!empty($detail)){
            if($type){
                if(empty($detail[0]['user']['password'])){
                    $where = "username = '".$detail[0]['user']['username']."',level='".$detail[0]['user']['level']."',nickname='".$detail[0]['nickname']."',
                    email='".$detail[0]['user']['email']."',phone='".$detail[0]['user']['phone']."',card='".$detail[0]['user']['iccard']."',group_id='".$detail[0]['user']['department']."',group_nickname='".$groupname."',is_admin='".$detail[0]['user']['is_audit']."',ip_adress='".$detail[0]['user']['ip_adress']."'";
                }else{
                    $where = "username = '".$detail[0]['user']['username']."',password='".$detail[0]['user']['password']."',level='".$detail[0]['user']['level']."',nickname='".$detail[0]['nickname']."',
                    email='".$detail[0]['user']['email']."',phone='".$detail[0]['user']['phone']."',card='".$detail[0]['user']['iccard']."',group_id='".$detail[0]['user']['department']."',group_nickname='".$groupname."',is_admin='".$detail[0]['user']['is_audit']."',ip_adress='".$detail[0]['user']['ip_adress']."'";
                }
                $dao=UserDao::sqlUpUser($where,$detail[0]['userid']);
                if($dao){
                    return true;
                }
            }else{
                $detail[0]['user']['groupname']=$groupname;
                $dao=UserDao::sqlAddUser($detail[0]);
                if($dao){
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 提交审核操作
     * @param int $type 是否允许通过审核 1:通过 0:禁止通过
     * @return boolean
     */
//    public function sql_update_user_audit($type){
//        if(!empty($this->audit_id)){
//            $row_user= $this->sql_audit_user();
//            if(!empty($type)){
//                if($row_user[0]['audit_aion'] == '3'){
//                    $sql= UserDao::sqlDelEnableUser($row_user[0]['userid']);
//                }elseif($row_user[0]['audit_aion'] == '2'){
//                    $sql=$this->sql_auth_where(1);
//                }elseif($row_user[0]['audit_aion'] == '1'){
//                    $sql=$this->sql_auth_where();
//                }elseif($row_user[0]['audit_aion'] == '4'){
//                    $sql=UserDao::sqlUpForbidden($row_user[0]['userid'],0);
//                }
//                if($sql){
//                    return array("log_id"=>$row_user[0]['log_id'],"id"=>$row_user[0]['id'],"type"=>"1");
//                }
//                return false;
//            }else{
//                return array("log_id"=>$row_user[0]['log_id'],"id"=>$row_user[0]['id'],"type"=>"1");
//            }
//        }
//        return array("log_id"=>$row_user[0]['log_id'],"type"=>"2");
//    }

        /**
     * 提交审核操作
     * @param int $type 是否允许通过审核 1:通过 0:禁止通过
     * @return boolean
     */
    public function sql_update_user_audit($type,$user){
        if(!empty($this->audit_id)){
            $row_user= $this->sql_audit_user();
            if(!empty($type)){
                $row_user[0]['user']['is_audit']=$user['is_audit'];
                $row_user[0]['user']['login_land']=empty($user['login_land'])?0:$user['login_land'];
                $group=UserDao::getDepartmentList($row_user[0]['user']['department']);
                $row_user[0]['user']['groupname']=$group[0]['nickname'];
                $dao=UserDao::sqlAddUser($row_user[0]);
                if($dao){
                    return array("log_id"=>$row_user[0]['log_id'],"id"=>$row_user[0]['id'],"type"=>"1");
                }
                return false;
            }else{
                return array("log_id"=>$row_user[0]['log_id'],"id"=>$row_user[0]['id'],"type"=>"1");
            }
        }
        return array("log_id"=>$row_user[0]['log_id'],"type"=>"2");
    }
}

?>
